package gui.storageunit;

import static org.junit.Assert.*;
import gui.inventory.ProductContainerData;

import org.junit.Before;
import org.junit.Test;

import persistence.DataPersistor;

import core.Container;
import core.ContainerFacade;

public class EditStorageUnitControllerTest implements IEditStorageUnitView {
	private String _name;
	private boolean _okEnabled;
	private EditStorageUnitController _curController;


	@Before
	public void setUp() throws Exception {
		DataPersistor.instance().init("-serial");
		ContainerFacade.clearAllData();
		ContainerFacade.instance().addStorageUnit("SU1");
		ContainerFacade.instance().addStorageUnit("SU2");
		ProductContainerData target = new ProductContainerData(
				ContainerFacade.instance().addStorageUnit("SU3"));
		
		_name = "";
		_okEnabled = false;
		_curController = new EditStorageUnitController(this, target);

	}

	@Test
	public void testLoadValues() {
		assertEquals("SU3",_name);
	}

	@Test
	public void testEnableComponents() {
		assertTrue(_okEnabled);
		
		_name = "SU1";
		_curController.enableComponents();
		assertFalse(_okEnabled);
		
		_okEnabled = true;
		_name = "SU2";
		_curController.enableComponents();
		assertFalse(_okEnabled);
		
		_okEnabled = true;
		_name = "";
		_curController.enableComponents();
		assertFalse(_okEnabled);
		
		_okEnabled = false;
		_name = "SU3";
		_curController.enableComponents();
		assertTrue(_okEnabled);		
		
		_okEnabled = false;
		_name = "new name";
		_curController.enableComponents();
		assertTrue(_okEnabled);				
	}

	@Test
	public void testValuesChanged() {
		assertTrue(_okEnabled);
		
		setStorageUnitName("SU1");
		assertFalse(_okEnabled);
		
		_okEnabled = true;
		setStorageUnitName("");
		assertFalse(_okEnabled);
	}

	@Test
	public void testEditStorageUnit() {
		setStorageUnitName("garbage");
		assertTrue(_okEnabled);
		
		_curController.editStorageUnit();
		
		Container su = ContainerFacade.instance().get_rootUnit().lookupContainer("garbage");
		assertNotNull(su);
		assertEquals("garbage",su.getName());
		assertNull(ContainerFacade.instance().get_rootUnit().lookupContainer("SU3"));
	}

	@Override
	public void displayInformationMessage(String message) {
		// not used		
	}

	@Override
	public void displayWarningMessage(String message) {
		// not used		
	}

	@Override
	public void displayErrorMessage(String message) {
		fail(message);		
	}

	@Override
	public String getStorageUnitName() {
		return _name;
	}

	@Override
	public void setStorageUnitName(String value) {
		_name = value;
		if (_curController != null)
			_curController.valuesChanged();		
	}

	@Override
	public void enableStorageUnitName(boolean value) {
		// not used		
	}

	@Override
	public void enableOK(boolean value) {
		_okEnabled = value;
	}

}
